package com.cvte.sunkit.manage

/**
 *  @PackageName com.cvte.sunkit.manage
 *  @Description  cpu异常数据的监听类，更新异常计数器和异常状态和阈值
 *  @Author lixu
 *  @Data 2023/7/19 12:17
 *  @Version 1.0
 */
object CpuMonitor {
    private var mCpuCount = 0 // 异常计数器
    private var mThresholdCpu = 95.0 // 百分比 默认值
    private var isAbnormal = false
    private var mAbnormalCnt = 0

    /**
     * 对外调用，更新count的值
     */
    fun updateCpuCount(sysCpuUsage: Double) {
        if (sysCpuUsage > mThresholdCpu) {
            addCpuCount()
            if (mCpuCount == 3) {
                isAbnormal = true
            }
        } else {
            subCpuCount()
            if (mCpuCount == 0) {
                isAbnormal = false
            }
        }
    }

    /**
     * 计数器加一
     */
    private fun addCpuCount() {
        mCpuCount = if (mCpuCount < 3) mCpuCount + 1 else 3
    }

    /**
     * 计数器减一
     */
    private fun subCpuCount() {
        mCpuCount = if (mCpuCount > 0) mCpuCount - 1 else 0
    }

    fun isAbnormal() = isAbnormal

    fun setThresholdCpu(threshold: Double) {
        mThresholdCpu = threshold
    }

    fun getThresholdCpu() = mThresholdCpu

    /**
     * 异常次数记录
     */
    fun addAbnormalCnt() {
        mAbnormalCnt++
    }

    fun getAbnormalCnt(): Int = mAbnormalCnt

    fun resetAbnormalCnt() {
        mAbnormalCnt = 0
    }

}